package team.tunan.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import team.tunan.common.Result;
import team.tunan.entity.Vote;
import team.tunan.entity.dto.MatchInfo;
import team.tunan.service.IMatchesService;
import team.tunan.service.IVoteService;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sqx
 * @since 2024-01-01
 */
@Controller
@RequestMapping("vote")
public class VoteController {
    @Autowired
    private IVoteService voteService;

    @Autowired
    private IMatchesService matchService;



    @RequestMapping("/page")
    public String score(Model model){
//        MatchInfo matchToShow = matchService.getMatchToShow(1);
//        model.addAttribute("info",matchToShow);


        return "voteList";
    }
    @RequestMapping("/pageShow/{mid}")
    @ResponseBody
    public ModelAndView scoreShow(@PathVariable Integer mid, Model model, HttpServletResponse response){
        MatchInfo matchToShow = matchService.getMatchToShow(mid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("info",matchToShow);
        modelAndView.setViewName("vote");
        return modelAndView;
    }

    @RequestMapping("/do")
    @ResponseBody
    public Result doVote(Vote vote){
        Map<String, Object> map = voteService.doVote(vote);
        boolean flag = (boolean) map.get("flag");
        return new Result(null,map,flag);
    }

}

